package com.jms.boot.listener.chanin;

import com.jms.framework.event.ApplicationStartEvent;
import com.jms.framework.listener.ApplicationListener;
import com.jms.framework.listener.ListenerChain;

import java.util.ArrayList;

public class ApplicationStartChain implements ListenerChain {

    private static volatile ApplicationStartChain instance;

    private final ArrayList<ApplicationListener<ApplicationStartEvent>> listeners;
    private final ApplicationStartEvent event;

    private ApplicationStartChain(ArrayList<ApplicationListener<ApplicationStartEvent>> listeners, ApplicationStartEvent event) {
        this.listeners = listeners;
        this.event = event;
    }

    public static ApplicationStartChain getInstance(ArrayList<ApplicationListener<ApplicationStartEvent>> listeners, ApplicationStartEvent event) {
        if (instance == null) {
            synchronized (ApplicationStartChain.class) {
                if (instance == null) {
                    instance = new ApplicationStartChain(listeners, event);
                }
            }
        }
        return instance;
    }
    @Override
    public void doChain() {
        for (ApplicationListener<ApplicationStartEvent> listener : listeners) {
            listener.onApplicationEvent(event);
        }
    }
}
